package com.zsl.swing.redis.desktop.window.panel;

import com.zsl.swing.redis.desktop.model.FontSizeConfigEntity;
import com.zsl.swing.redis.desktop.model.OftenUseKeyModel;
import com.zsl.swing.redis.desktop.model.OtherGeneralConfigEntity;
import com.zsl.swing.redis.desktop.px.PxTextArea;
import com.zsl.swing.redis.desktop.type.DbOptEnum;
import com.zsl.swing.redis.desktop.utils.*;
import com.zsl.swing.redis.desktop.window.ZslRedisDesktopMainWindow;
import com.zsl.swing.redis.desktop.window.list.ZslKeyList;
import com.zsl.swing.redis.desktop.window.list.oftenusekey.ZslOfenUseKeyPatternList;
import com.zsl.swing.redis.desktop.window.menu.ZslMenuItem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

public class ZslKeyQueryPanel extends JPanel {

//    private final JTextArea queryTextField = new JTextArea(5,40);
    private final JTextArea queryTextField = new PxTextArea(500, 200);

    private final JComboBox<String> actionBox = this.buildComboBox();

    private final ZslKeyList keyList;

    private final ZslOfenUseKeyPatternList ofenUseKeyPatternList;

    public ZslKeyQueryPanel(ZslDbOptPanel parent) {
        this.setLayout(new BorderLayout());

        FontSizeConfigEntity fontSizeConfig = ZslRedisDesktopMainWindow.getFontSizeConfig();
        Font font = FontUtils.getFont(fontSizeConfig.getFontName(), Font.PLAIN, fontSizeConfig.getTree());

        this.queryTextField.setLineWrap(true);
        this.queryTextField.setFont(font);
        this.queryTextField.setBorder(BorderFactory.createTitledBorder("查询条件"));

        this.keyList = new ZslKeyList(parent.getEntity(), parent.getValueShowPanel());
        this.keyList.setFont(font);

        this.ofenUseKeyPatternList = new ZslOfenUseKeyPatternList(parent.getEntity(), this.queryTextField);
        this.ofenUseKeyPatternList.setFont(font);

        JScrollPane keyScrollPane = new JScrollPane(this.keyList);
        keyScrollPane.setBorder(BorderFactory.createTitledBorder("keys"));

        JScrollPane oftenUseScrollPane = new JScrollPane(this.ofenUseKeyPatternList);
        oftenUseScrollPane.setBorder(BorderFactory.createTitledBorder("常用key集合"));

        JPanel queryPanel = new JPanel();
        queryPanel.add(new JScrollPane(this.queryTextField, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));

        JPanel optPanel = new JPanel();
        optPanel.add(this.actionBox);

        JButton runBtn = new JButton("执行(run)");
        JButton stopBtn = new JButton("停止");
        stopBtn.setToolTipText("停止运行中耗时任务");

        JButton clearBtn = new JButton("清空已删除文件");
        clearBtn.setToolTipText("删除当前连接下所有已执行过的已删除的keys文件");

        JButton oftenUseKeysBtn = new JButton("设置为常用key");
        oftenUseKeysBtn.setToolTipText("添加到常用key集合");



        optPanel.add(runBtn);
        optPanel.add(stopBtn);
        optPanel.add(oftenUseKeysBtn);
        optPanel.add(clearBtn);

        JPanel northPanel = new JPanel();
        northPanel.setLayout(new BoxLayout(northPanel,BoxLayout.Y_AXIS));
        northPanel.add(queryPanel);
        northPanel.add(optPanel);

        this.add(northPanel, BorderLayout.NORTH);

        Box centerPanel  = Box.createVerticalBox();
        centerPanel.add(keyScrollPane);
        centerPanel.add(oftenUseScrollPane);

        this.add(centerPanel, BorderLayout.CENTER);
//        this.add(keyScrollPane, BorderLayout.CENTER);
//        this.add(oftenUseScrollPane, BorderLayout.SOUTH);

        runBtn.addActionListener(event -> this.runBtnAction());
        stopBtn.addActionListener(event -> this.stopBtnAction(stopBtn));
        clearBtn.addActionListener(event -> this.clearAllDeletedKeysFile());

        oftenUseKeysBtn.addActionListener(event -> this.oftenUserKeyAction());
    }

    private void oftenUserKeyAction() {
        String keyword = this.queryTextField.getText();

        if(StringUtils.isEmpty(keyword)){
            DialogUtils.errorDialog("查询条件为空");
            return;
        }


        this.ofenUseKeyPatternList.addNode(keyword.trim());


    }

    private void clearAllDeletedKeysFile() {

        boolean select = DialogUtils.confirmDialog(ZslRedisDesktopMainWindow.getMainWindow(), "确定要清空历史已删除的keys文件么？");
        if(select){
            LongTimeTaskUtils.clearDeletedKeyFileAll(this.keyList.getDbEntity().getUniqueId());
        }
    }

    public void stopBtnAction(JButton stopBtn ){

        List<LongTimeTaskUtils.LongTimeTask> processingTaskList = LongTimeTaskUtils.getProcessingTaskList();

        if(CollectionUtils.isEmpty(processingTaskList)){
            DialogUtils.msgDialog(ZslRedisDesktopMainWindow.getMainWindow(), "当前无进行中的操作");
            return;
        }



        JPopupMenu popupMenu = new JPopupMenu();

        for (LongTimeTaskUtils.LongTimeTask longTimeTask : processingTaskList) {
            popupMenu.add(ZslMenuItem.getInstance(longTimeTask.getTaskName(), 1000,  event -> {
                LongTimeTaskUtils.cancel(event.getActionCommand());

//                DialogUtils.msgDialog(ZslRedisDesktopMainWindow.getMainWindow(), "取消任务成功");
            }));

        }

        popupMenu.show(stopBtn,stopBtn.getX(),stopBtn.getY());




    }

    private void runBtnAction() {
        String actionCommand = (String)this.actionBox.getSelectedItem();
        DbOptEnum command = DbOptEnum.locate2Enum(actionCommand);

        String keyword = this.queryTextField.getText();
        if(!StringUtils.isEmpty(keyword)){
            keyword = keyword.trim();
        }

        ZslRedisDesktopMainWindow.getZslLogPanel().log("当前查询关键词：" + keyword);

        OtherGeneralConfigEntity otherGeneralConfig = ZslRedisDesktopMainWindow.getOtherGeneralConfig();
        switch (command){
            case SHOW_ALL:{
                keyword = "*";
                this.keyList.queryLikeList(keyword, null, otherGeneralConfig.getShowALLScanCount(), true);
                break;
            }
            case LIKE:{
                this.keyList.queryLikeList(keyword, null, otherGeneralConfig.getQueryLikeScanCount(), true);
                break;
            }
            case QUERY: {
                this.keyList.queryOne(keyword);
                break;
            }
            case DELETE: {
                this.keyList.deleteOne(keyword);
                break;
            }
            case DELETE_LIKE: {
                this.keyList.deleteLike(keyword, otherGeneralConfig.getDeleteLikeScanCount());
                break;
            }
            default:break;
        }
    }


    private JComboBox<String> buildComboBox(){
        JComboBox<String> actionBox = new JComboBox<>();
        actionBox.addItem(DbOptEnum.LIKE.getText());
        actionBox.addItem(DbOptEnum.QUERY.getText());
        actionBox.addItem(DbOptEnum.DELETE_LIKE.getText());
        actionBox.addItem(DbOptEnum.DELETE.getText());
        actionBox.addItem(DbOptEnum.SHOW_ALL.getText());
        return actionBox;
    }

    public ZslKeyList getKeyList(){
        return this.keyList;
    }

}
